import {Component} from '@angular/core';
import {App, Events, IonicPage, ModalController, NavController} from 'ionic-angular';
import {DialogProvider} from "@providers/dialog";
import {StorageProvider} from "@providers/storage";
import {env} from "@env/environment";
import {WorkerProvider} from "@providers/interface/worker";

/**
 * Generated class for the WorkerMyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-worker-my',
  templateUrl: 'worker-my.html',
})
export class WorkerMyPage {

  userInfo: any;
  SumNum: any = {
    "SPSL": "0",//完工记录
    "PRICE": "0",//金额
    "chargesSum": "0"
  };//获取安装总数/预计提成
  isWorking: boolean = false;//是否开工
  materialCosts: any = [];//辅材费用
  product_id = 0;
  SERVICETYPEID = 0;
  fixedCosts: any = [];//固定费用

  dev: boolean;

  constructor(private app: App, private dialog: DialogProvider,
              private storage: StorageProvider, public navCtrl: NavController, private modalCtrl: ModalController,
              private worker: WorkerProvider,
              private events: Events) {
    this.userInfo = this.storage.read("userInfo");
    this.isWorking = this.userInfo.SFZX == '10';
    this.dev = env.mode == 'prod';
    //  console.log(this.userInfo)
  }

  push(page: string) {
    this.navCtrl.push(page);
  }

  ionViewWillEnter() {
    this.userInfo = this.storage.read("userInfo");
    this.isWorking = this.userInfo.SFZX == '10';
    this.getSumNum();
  }

  /**
   * 更改开工状态
   */
  switchIsWorking() {
    let SFZX = null;
    if (this.isWorking) {
      SFZX = '10';
    } else {
      SFZX = '0';
    }
    this.worker.switchIsWorking(this.userInfo["PARTYID"], SFZX)
      .then(v => {
        this.userInfo.SFZX = v.data["SFZX"];
        this.storage.write('userInfo', this.userInfo);
      })
      .catch(e => {
      });
  }

  logout(page: string) {
    this.dialog.showConfirm(() => {
      this.storage.write("userInfo", {});
      this.app.getRootNav().setRoot(page);
      this.events.publish('logout');
    }, "是否退出登录？")
  }

  //获取安装总数/预计提成
  getSumNum() {
    this.worker.getSumNum({"WORKER": this.userInfo["PARTYID"], "EXTID": this.userInfo["EXTID"]})
    //item:{"WORKER": this.userInfo["PARTYID"], "FWLX": "2"}
      .then(
        message => {
          if (Object.keys(message.data).length != 0) {
            this.SumNum = message.data;
          }
        })
      .catch(error => {
        this.dialog.showAlert(error);
      });
  }

  //  ionViewCanEnter(): boolean {debugger;
  //   if (!this.authGuard.checkLogin('RuserPage', this.navCtrl)) { //"UserPage"
  //     // this.navCtrl.push("LoginPage");
  //     this.modalCtrl.create('LoginPage', '', { showBackdrop: false }).present();
  //     return false;
  //   }
  //   return true;
  // }

  showMaterialModal() {
    let modal = this.modalCtrl.create('SazytMmaterialPage',
      {
        "materialCosts": this.materialCosts,
        "product_id": this.product_id,
        "SERVICETYPEID": this.SERVICETYPEID
      });
    modal.onDidDismiss(data => {
      if (data) {
        let isError = false;
        this.fixedCosts.forEach(value => {
          data.forEach(val => {
            if (value.PRODUCT_ID == val.PRODUCT_ID) {
              isError = true;
            }
          });
        });
        if (isError) {
          this.dialog.showAlert("该商品已有的辅材不允许额外多选！");
          return;
        }
        this.materialCosts = data;
        // this.calculateSum();
      }
    });
    modal.present();
  }

  test() {
    let modal = this.modalCtrl.create("SignaturePadPage");
    modal.present()
  }

  Access() {
    this.navCtrl.push("CostListPage");
  }

}
